package com.xdwanj.geoquiz.activity;

import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProviders;

import com.xdwanj.geoquiz.R;
import com.xdwanj.geoquiz.live.QuizViewModel;

public class MainActivity extends AppCompatActivity {

    private final static String TAG = "MainActivity";
    private final static String KEY_INDEX = "index";
    private final static String EXTRA_ANSWER_IS_TRUE = "com.xdwanj.geoquiz.answerIsTrue";
    private static final int REQUEST_CODE_CHEAT = 0;

    private Button buttonTrue;
    private Button buttonFalse;
    private ImageButton Next;
    private ImageButton pre;
    private TextView questionText;
    private Button cheatBtn;

    private QuizViewModel quizViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "onCreate"); //日志

        //获取ViewModel对象并绑定
        quizViewModel = ViewModelProviders.of(this).get(QuizViewModel.class);
        Log.d(TAG, "onCreate: quizViewModel->+" + this.quizViewModel.toString() + " is Got"); //打印日志

        //获取控件
        questionText = findViewById(R.id.questionText);
        buttonTrue = findViewById(R.id.btn1);
        buttonFalse = findViewById(R.id.btn2);
        Next = findViewById(R.id.next);
        pre = findViewById(R.id.pre);
        cheatBtn = findViewById(R.id.cheat_button);

        //设置正确按钮事件
        buttonTrue.setOnClickListener(view -> checkAnswer(true));

        //设置错误按钮事件
        buttonFalse.setOnClickListener(view -> checkAnswer(false));

        //下一个事件
        Next.setOnClickListener(view -> questionText.setText(this.quizViewModel.moveNext()));

        //上一个事件
        pre.setOnClickListener(view -> questionText.setText(this.quizViewModel.movePre()));

        //问题文本事件
        questionText.setOnClickListener(view -> questionText.setText(this.quizViewModel.moveNext()));

        //查看答案按钮事件
        cheatBtn.setOnClickListener(view -> {
            // 去往作弊页面
            Intent intent = CheatActivity.newIntent(this, quizViewModel.getCurrentQuestionAnswer());

            //版本判断
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                ActivityOptions activityOptions = ActivityOptions.makeClipRevealAnimation(view, 0, 0, view.getWidth(), view.getHeight());
                startActivityForResult(intent, REQUEST_CODE_CHEAT, activityOptions.toBundle());
            } else {
                startActivityForResult(intent, REQUEST_CODE_CHEAT);
            }

        });

        //未实现
        try {
            this.quizViewModel.currentIndex = savedInstanceState.getInt(KEY_INDEX);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //设置问题文本
        questionText.setText(this.quizViewModel.getCurrentQuestionText());
    }

    /**
     * 判断答案
     *
     * @param userAnswer 用户的答案
     */
    private void checkAnswer(boolean userAnswer) {
        int message = R.string.incorrect_toast;

        if (quizViewModel.isCheater) {
            message = R.string.judgment_toast;
        } else if (userAnswer == quizViewModel.getCurrentQuestionAnswer()) {
            message = R.string.hello;
        }

        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    /**
     * 回调页面函数
     *
     * @param requestCode 请求码
     * @param resultCode  结果码
     * @param data        数据
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        //请求码和结果码都没问题继续执行
        if (resultCode != RESULT_OK || requestCode != REQUEST_CODE_CHEAT) {
            return;
        }

        //非空判断
        if (data != null) {
            quizViewModel.isCheater =
                    data.getBooleanExtra(CheatActivity.EXTRA_ANSWER_SHOWN, false);
        }
    }

    //用户主页键回到桌面调用
    @Override
    public void onSaveInstanceState(@NonNull Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        Log.d(TAG, "onSaveInstanceState");
        savedInstanceState.putInt(KEY_INDEX, quizViewModel.currentIndex);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause");
    }

}